package com.t.bricks.bussiness.component.clientmanage;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.t.bricks.bussiness.db.dao.clientmanage.ClientManageDao;
import com.t.bricks.bussiness.db.entity.clientmanage.ClientManage;
import com.t.bricks.bussiness.model.clientmanage.Orders;
import com.t.bricks.utils.function.json.JacksonShell;
import com.t.bricks.utils.spring.ModuleUtil;

/**
 * 用切面方式监听业务模块,然后执行'客户信息'相关操作
 * 注意:如非必要,请勿使用
 */
@Aspect
@Component
public class ClientManageServiceAspect {
	
	@Autowired
	private ClientManageDao clientManageDao;
	
	/**
	 * 定义切入点，切入点为'返回类型 被切入的实现类.切入的函数(函数参数类型)'
	 * 1、execution(public * *(..)) 任意的公共方法
	 * 2、execution(* set*(..)) 以set开头的所有的方法
	 * 3、execution(* com.lingyejun.annotation.LoggerApply.*(..))com.lingyejun.annotation.LoggerApply这个类里的所有的方法
	 * 4、execution(* com.lingyejun.annotation.*.*(..))com.lingyejun.annotation包下的所有的类的所有的方法
	 * 5、execution(* com.lingyejun.annotation..*.*(..))com.lingyejun.annotation包及子包下所有的类的所有的方法
	 * 6、execution(* com.lingyejun.annotation..*.*(String,?,Long)) com.lingyejun.annotation包及子包下所有的类的有三个参数，第一个参数为String类型，第二个参数为任意类型，第三个参数为Long类型的方法
	 * 7、execution(@annotation(com.lingyejun.annotation.Lingyejun))
	 */
	@Pointcut(value = "execution(com.t.bricks.model.MsgEmity com.t.bricks.bussiness.service.orders.impl.OrdersServiceImpl*.add(Object))")
	public void doMethod() {
	}
	
	/**
	 * 前置通知
	 * @param joinpoint
	 */
	@Before("doMethod()")
	public void beforeReturning(JoinPoint joinpoint) {
		Object[] methodArgs = joinpoint.getArgs();
		if (methodArgs.length < 1 || null == methodArgs[0]) {
			return;
		}

		String sCreator = ModuleUtil.currentLoginUserId();
		Object methodArg = methodArgs[0];
			
		ModuleUtil.runThread(() -> {//多线程方式重新构造缓存数据
			Orders orders = JacksonShell.toJavaObject(methodArg, Orders.class);
			if (null == orders) {
				return;
			}

			String sId = ModuleUtil.getNewId(8, "ClientManage");
			ClientManage clientManage = new ClientManage();
			clientManage.setsId(sId);
			clientManage.setsName(orders.getsClient());
			clientManage.setsPhone(orders.getsClientPhone());
			clientManage.setsAliases(orders.getsClientAliases());
			clientManage.setiSex(orders.getiClientSex());
			clientManage.setsPhone(orders.getsClientPhone());
			clientManage.setsAddress(orders.getsClientAddress());
			clientManage.setiLeven(orders.getiClientLeven());
			clientManage.setiBlackList(orders.getiClientBlackList());
			clientManage.setsCreator(sCreator);
			clientManage.setsModifieder(sCreator);
			clientManage.setsMemo(orders.getsClientMemo());
			
			clientManageDao.addAop(clientManage);
		});
	}
	
}
